//
//  CustomNavigationBar.m
//  XMJJ_zml
//
//  Created by 杨何华 on 2025/5/26.
//

#import "CustomNavigationBar.h"
#import <Masonry/Masonry.h>

@implementation CustomNavigationBar


- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupView];
    }
    return self;
}

- (void)setupView {
    self.backgroundColor = [UIColor whiteColor];
    
    // 返回按钮
    self.backButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.backButton setImage:[UIImage imageNamed:@"back_icon"] forState:UIControlStateNormal];
    [self.backButton setTitle:@"返回" forState:UIControlStateNormal];
    [self.backButton addTarget:self action:@selector(backButtonTapped) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.backButton];
    
    // 标题标签
    self.titleLabel = [[UILabel alloc] init];
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    self.titleLabel.font = [UIFont boldSystemFontOfSize:16];
    [self addSubview:self.titleLabel];
    
    // 底部横线
    self.bottomLine = [[UIView alloc] init];
    self.bottomLine.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1.0];
    [self addSubview:self.bottomLine];
    
    // 设置约束
    [self.backButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(15);
        make.centerY.equalTo(self).offset(-8);
        make.top.equalTo(@30);
        make.height.equalTo(@40);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.centerY.equalTo(self.backButton);
        make.left.greaterThanOrEqualTo(self.backButton.mas_right).offset(10);
    }];
    
    [self.bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self);
        make.height.equalTo(@0.5);
    }];
}

- (void)backButtonTapped {
    if (self.backAction) {
        self.backAction();
    }
}
- (void)setIsHiddenOnButton:(BOOL)isHiddenOnButton
{
    _isHiddenOnButton = isHiddenOnButton;
    self.backButton.hidden = isHiddenOnButton;
}
- (void)setTitle:(NSString *)title {
    self.titleLabel.text = title;
}

@end
